<template>
  <transition name="snackbar">
    <div
      v-if="visible"
      class="snackbar"
      :class="[type]"
    >
      <div class="snackbar-content">
        <span class="snackbar-message">{{ message }}</span>
      </div>
    </div>
  </transition>
</template>

<script lang="ts" setup>
import { watch } from 'vue';

interface Props {
  visible: boolean;
  message: string;
  type?: 'success' | 'error' | 'info';
  duration?: number;
}

const props = withDefaults(defineProps<Props>(), {
  type: 'success',
  duration: 3000
});

const emit = defineEmits<{
  hide: []
}>();

// 自动隐藏逻辑
watch(() => props.visible, (newVisible) => {
  if (newVisible && props.duration > 0) {
    setTimeout(() => {
      emit('hide');
    }, props.duration);
  }
});
</script>

<style scoped>
.snackbar {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  min-width: 300px;
  max-width: 90vw;
  padding: 12px 24px;
  border-radius: 8px;
  color: white;
  font-size: 14px;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.snackbar.success {
  background: #4caf50;
}

.snackbar.error {
  background: #f44336;
}

.snackbar.info {
  background: #2196f3;
}

.snackbar-content {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.snackbar-message {
  font-weight: 500;
}

/* 动画效果 */
.snackbar-enter-active,
.snackbar-leave-active {
  transition: all 0.3s ease;
}

.snackbar-enter-from {
  opacity: 0;
  transform: translate(-50%, 100%);
}

.snackbar-leave-to {
  opacity: 0;
  transform: translate(-50%, 100%);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .snackbar {
    min-width: 280px;
    margin: 0 20px;
  }
}
</style>